package com.bst.bsj.bops.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.AreaService;
import com.bst.bsj.bops.web.biz.SeedlingService;
import com.bst.bsj.common.dal.dataobject.Area;
import com.bst.bsj.common.dal.dataobject.Seedling;
import com.bst.bsj.common.maopu.enums.SeedlingStatus;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.StringUtil;

@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/seedling")
public class SeedlingController extends WebBaseController {

  @Autowired
  private SeedlingService seedlingService;

  @Autowired
  private AreaService areaService;

  /**
   * 
  * @Title: auditList
  * @Description: TODO(这里用一句话描述这个方法的作用)
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月14日 上午11:43:39
  * @modificator :tonglu
  * @modify date :2016年5月14日 上午11:43:39
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(value = "/seedlingList")
  public ModelAndView auditList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    ModelAndView modelAndView = getBopsModelAndView("goods/goods");
    String baseName = (String) params.get("baseName");//名称
    String cityNumber = (String) params.get("cityNumber");
    String provinceNumber = (String) params.get("provinceNumber");
    String scName = (String) params.get("scName");
    String plantCategory = (String) params.get("plantCategory");
    if (baseName != null) {
      baseName = baseName.trim();
    }
    if ("全部".equals(plantCategory)) {
      plantCategory = "";
    }

    if (scName != null) {
      scName = scName.trim();
    }
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    if (StringUtil.isNotBlank(plantCategory)) {
      paramsMap.put("plantCategory", plantCategory);
    }
    if (StringUtil.isNotBlank(baseName)) {
      paramsMap.put("baseName", baseName);
    }
    if (StringUtil.isNotBlank(provinceNumber)) {
      paramsMap.put("provinceNumber", provinceNumber);
    }
    if (StringUtil.isNotBlank(cityNumber)) {
      paramsMap.put("cityNumber", cityNumber);
    }
    if (StringUtil.isNotBlank(scName)) {
      paramsMap.put("scName", scName);
    }
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(10));
    paramsMap.put("offset", new Long((pageNum - 1) * 10));
    Result result = seedlingService.query(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<Seedling> seedlingList = (List<Seedling>) result.getModels().get("seedlingList");
      modelAndView.addObject("seedlingList", seedlingList);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);
    List<Area> provinceList = areaService.getAreas("province"); //所有省
    modelAndView.addObject("provinceList", provinceList);
    return modelAndView;
  }

  /**
   * 
  * @Title: getSeedlingCompany
  * @Description: TODO(根据id获取对象)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午3:14:01
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午3:14:01
   */
  @RequestMapping(value = "/getSeedling")
  public ModelAndView getSeedlingCompany(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    ModelAndView modelAndView = new ModelAndView();
    Seedling seedling = seedlingService.getSeedling(Long.valueOf(id));
    modelAndView.addObject("seedling", seedling);
    return modelAndView;
  }

  /**
   * 
  * @Title: seedling
  * @Description: 热门苗木管理
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * String
  * @throws 
  * @creator     :yj
  * @create date :2016年5月17日 下午5:23:22
  * @modificator :yj
  * @modify date :2016年5月17日 下午5:23:22
   */
  @RequestMapping(value = "/seedling")
  public ModelAndView seedling(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    ModelAndView modelAndView = getBopsModelAndView("management/seedling");
    Map<String, Object> paramsMap = new HashMap<String, Object>();

    String baseName = (String) params.get("baseName");//名称
    String provinceNumber = (String) params.get("provinceNumber");
    String cityNumber = (String) params.get("cityNumber");
    String scName = (String) params.get("scName");
    String plantCategory = (String) params.get("plantCategory");
    if (baseName != null) {
      baseName = baseName.trim();
    }
    if ("全部".equals(plantCategory)) {
      plantCategory = "";
    }

    if (scName != null) {
      scName = scName.trim();
    }
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    if (StringUtil.isNotBlank(plantCategory)) {
      paramsMap.put("plantCategory", plantCategory);
    }
    if (StringUtil.isNotBlank(baseName)) {
      paramsMap.put("baseName", baseName);
    }
    if (StringUtil.isNotBlank(provinceNumber)) {
      paramsMap.put("provinceNumber", provinceNumber);
    }
    if (StringUtil.isNotBlank(cityNumber)) {
      paramsMap.put("cityNumber", cityNumber);
    }
    if (StringUtil.isNotBlank(scName)) {
      paramsMap.put("scName", scName);
    }
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("isHot", 1); //热门
    paramsMap.put("status", SeedlingStatus.OFF.getCode()); //非下架状态
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(50));
    paramsMap.put("offset", new Long((pageNum - 1) * 50));

    Result result = seedlingService.queryHots(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<Seedling> seedlingList = (List<Seedling>) result.getModels().get("seedlingList");

      modelAndView.addObject("seedlingList", seedlingList);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);

    List<Area> provinceList = areaService.getAreas("province"); //所有省
    modelAndView.addObject("provinceList", provinceList);

    return modelAndView;
  }

  /**
   * 
  * @Title: getSeedlingCompany
  * @Description: TODO(根据id修改状态)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午3:14:01
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午3:14:01
   */
  @RequestMapping(value = "/changeStatus")
  @ResponseBody
  public String changeStatus(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    String status = (String) params.get("status");//状态
    String isHot = (String) params.get("isHot");//状态
    Seedling seedling = new Seedling();
    seedling.setId(Long.valueOf(id));
    if (status != null) {
      seedling.setStatus(status);
      seedling.setIsHot(0);
    }
    if (isHot != null) {
      Integer ishot = Integer.valueOf(isHot);
      seedling.setIsHot(ishot);
      if (ishot == 1) {
        Long seq = seedlingService.getMaxSeq();
        if (seq == null) {
          seq = 0l;
        }
        if (seq < 50) {
          seedling.setSeq(seq + 1);
        }
      } else {
        seedling.setSeq(null);
      }

    }
    seedlingService.update(seedling);
    return "success";
  }

  /**
  * @Title: toSort
  * @Description: 保存热门排序
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * String
  * @throws 
  * @creator     :yj
  * @create date :2016年5月18日 下午4:28:41
  * @modificator :yj
  * @modify date :2016年5月18日 下午4:28:41
   */
  @RequestMapping(value = "/toSort")
  public ModelAndView toSort(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);
    String idStr = (String) params.get("id");
    String seqStr = (String) params.get("seq");
    String oldSeq = (String) params.get("oldSeq");

    if (StringUtil.isNotBlank(idStr) && StringUtil.isNotBlank(seqStr)) {
      if (oldSeq.equals(seqStr)) {
        return new ModelAndView("redirect:seedling.htm");
      }

      Long seq = Long.parseLong(seqStr);
      List<Seedling> aHotsList = seedlingService.getAfterHots(params);
      seedlingService.updateSort(params);
      for (int i = 0; i < aHotsList.size(); i++) {
        Seedling seedling = aHotsList.get(i);
        Long sSeq = seedling.getSeq();
        Long id = seedling.getId();
        if (id == Long.parseLong(idStr)) {
          continue;
        }

        params.put("seq", sSeq + 1);
        params.put("id", seedling.getId());

        if (sSeq >= 50) {
          params.put("seq", null);
          seedlingService.updateSort(params);
        }

        if (seq < sSeq) {
          break;
        }
        seedlingService.updateSort(params);

        Long sSeq2 = null;
        if (i < aHotsList.size() - 1) {
          Seedling seedling2 = aHotsList.get(i + 1);
          sSeq2 = seedling2.getSeq();
          if (sSeq < sSeq2 - 1) {
            break;
          }
        }
        seq++;

      }

    }

    return new ModelAndView("redirect:seedling.htm");
  }

}
